package com.jihox.abookpro;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import com.jihox.abookpro.util.PictureUtil;

/**
 * 
 * @{# SplashActivity.java Create on 2013-5-2 下午9:10:01
 * 
 *     class desc: 启动画面 (1)判断是否是首次加载应用--采取读取SharedPreferences的方法
 *     (2)是，则进入GuideActivity；否，则进入MainActivity (3)3s后执行(2)操作
 * 
 *     <p>
 *     Copyright: Copyright(c) 2013
 *     </p>
 * @Version 1.0
 * 
 * 
 */
public class SplashActivity extends BaseActivity {
	int preVersionCode = 0;//上一次进入程序的版本号
	private int curVersionCode=0;//当前版本号
	private static String cstToken = null;
	
	private static final int GO_HOME = 1000;
	private static final int GO_GUIDE = 1001;
//	private static final int GO_LOGIN = 1002;
	// 延迟1.5秒
	private static final long SPLASH_DELAY_MILLIS = 1500;

	private static final String SHAREDPREFERENCES_NAME = "first_pref";
	private static final String SHAREDPREFERENCES_CUSTOMERINFO = "CUSTOMER_TOKEN";
	private ImageView ivHome = null;
	
	/**
	 * Handler:跳转到不同界面
	 */
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case GO_HOME:
				goHome();
				break;
			case GO_GUIDE:
				goGuide();
//				goHome();
				break;
//			case GO_LOGIN:
//				goLogin();
			}
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);
		
		ivHome = (ImageView)findViewById(R.id.ivHome);
		Bitmap bm =PictureUtil.readBitMap(getApplicationContext(), R.drawable.splash);
		BitmapDrawable bd = new BitmapDrawable(this.getResources(), bm);
		ivHome.setBackgroundDrawable(bd);
		
		Intent intent = this.getIntent();//Add FromWhere(从合作方软件进入)
		String from = intent.getStringExtra("FromWhere");
		if(from!=null&&!from.equals("")){
			Toast.makeText(this,from, Toast.LENGTH_SHORT).show();
		}
		init();
//		sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
	}

	private void init() {
		// 读取SharedPreferences中需要的数据
		// 使用SharedPreferences来记录程序上一次进入时的版本
		SharedPreferences preferences = getSharedPreferences(
				SHAREDPREFERENCES_NAME, MODE_PRIVATE);
		// 取得相应的值，如果没有该值，说明还未写入，用0作为默认值
		preVersionCode = preferences.getInt("versionCode", 0);
		//取得cstToken，如果有值，则GO_HOME;没有值，怎转到注册界面GO_SIGNUP
		SharedPreferences preferencesToken = getSharedPreferences(
				SHAREDPREFERENCES_CUSTOMERINFO, MODE_PRIVATE);
		cstToken = preferencesToken.getString("cstToken", "");
		Log.d("cstToken", cstToken);
		
		try {
			PackageInfo info = this.getPackageManager().getPackageInfo(
					this.getPackageName(), 0);
			curVersionCode = info.versionCode;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 判断程序的版本号，如果与当前程序版本不一致跳转到引导界面，否则跳转到主界面
		if (curVersionCode==preVersionCode) {
			mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
			// 使用Handler的postDelayed方法，3秒后执行跳转到MainActivity
			//如果cstToken不为空，则直接进入Home,否则进去注册页
//			if (cstToken == null || cstToken.isEmpty()) {
//				mHandler.sendEmptyMessageDelayed(GO_LOGIN, SPLASH_DELAY_MILLIS);
//			}else {
//				mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
//			}			
		} else {
			mHandler.sendEmptyMessageDelayed(GO_GUIDE, SPLASH_DELAY_MILLIS);
		}

	}

	private void goHome() {
		Intent intent = new Intent(SplashActivity.this, HomeChangeActivity.class);
		intent.putExtra("ComeFrom", "Start");
		intent.putExtra("FragmentIndex", 0);
		SplashActivity.this.startActivity(intent);
		SplashActivity.this.finish();
	}
	//进入引导界面
	private void goGuide() {
		Intent intent = new Intent(SplashActivity.this, GuideActivity.class);
		intent.putExtra("curVersionCode", curVersionCode);
		SplashActivity.this.startActivity(intent);
		SplashActivity.this.finish();
	}
//	//进入注册页面
//	private void goSignUp() {
//		Intent intent = new Intent(SplashActivity.this, SignUpActivity.class);
//		SplashActivity.this.startActivity(intent);
//		SplashActivity.this.finish();
//	}
//	//进去登录界面
//	private void goLogin() {
//		Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
//		SplashActivity.this.startActivity(intent);
//		SplashActivity.this.finish();
//	}
	
	@Override
	protected void onDestroy(){
		super.onDestroy();
		BitmapDrawable bd = (BitmapDrawable)ivHome.getBackground();
		ivHome.setBackgroundResource(0);

		bd.setCallback(null);
		bd.getBitmap().recycle();
		System.gc();
	}
}
